package com.gamerole.prepare.listener;

import com.gamerole.prepare.Constant;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.xuxiaoxiao.chatapi.wechat.WeChatClient;
import me.xuxiaoxiao.chatapi.wechat.entity.contact.WXContact;
import me.xuxiaoxiao.chatapi.wechat.entity.contact.WXGroup;
import me.xuxiaoxiao.chatapi.wechat.entity.contact.WXUser;
import me.xuxiaoxiao.chatapi.wechat.entity.message.WXMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;

public class WeChatListener extends WeChatClient.WeChatListener {
    @Autowired
    StringRedisTemplate redisTemplate;
    public static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create();

    @Override
    public void onQRCode(@Nonnull WeChatClient client, @Nonnull String qrCode) {
        System.out.println("onQRCode：" + qrCode);
        redisTemplate.opsForValue().set(Constant.QR_CODE_URL, qrCode);
    }

    @Override
    public void onLogin(@Nonnull WeChatClient client) {
        HashMap<String, WXGroup> stringWXGroupHashMap = client.userGroups();

        HashMap<String, WXUser> userFriends = client.userFriends();
        userFriends.forEach((s, wxUser) -> redisTemplate.opsForValue().set(wxUser.name,wxUser.id));
        stringWXGroupHashMap.forEach((s, wxGroup) -> System.out.println(s + "---->" + wxGroup.name));
        System.out.println(String.format("onLogin：您有%d名好友、活跃微信群%d个", client.userFriends().size(), client.userGroups().size()));
    }

    @Override
    public void onMessage(@Nonnull WeChatClient client, @Nonnull WXMessage message) {
        System.out.println("获取到消息：" + GSON.toJson(message));

//            if (message instanceof WXVerify) {
//                //是好友请求消息，自动同意好友申请
//                client.passVerify((WXVerify) message);
//            } else if (message instanceof WXLocation && message.fromUser != null && !message.fromUser.id.equals(client.userMe().id)) {
//                // 如果对方告诉我他的位置，发送消息的不是自己，则我也告诉他我的位置
//                if (message.fromGroup != null) {
//                    // 群消息
//                    // client.sendLocation(message.fromGroup, "120.14556", "30.23856", "我在这里", "西湖");
//                } else {
//                    // 用户消息
//                    client.sendLocation(message.fromUser, "120.14556", "30.23856", "我在这里", "西湖");
//                }
//            } else if (message instanceof WXText && message.fromUser != null && !message.fromUser.id.equals(client.userMe().id)) {
//                //是文字消息，并且发送消息的人不是自己，发送相同内容的消息
//                if (message.fromGroup != null) {
//                    // 群消息
//                    // client.sendText(message.fromGroup, message.content);
//                } else {
//                    // 用户消息
//                    client.sendText(message.fromUser, message.content);
//                }
//            }
    }

    @Override
    public void onContact(@Nonnull WeChatClient client, @Nullable WXContact oldContact, @Nullable WXContact newContact) {
        System.out.println(String.format("检测到联系人变更:旧联系人名称：%s:新联系人名称：%s", (oldContact == null ? "null" : oldContact.name), (newContact == null ? "null" : newContact.name)));
    }

    @Override
    public void onLogout(@Nonnull WeChatClient client) {
        System.out.println("退出了");
        super.onLogout(client);
        client=null;
    }
}
